from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.elements import BinaryExpression
from SqlData.Repository import *
from SqlData.DataEntity import *
from Lib.Utils import *
import datetime


#用户业务类
class BllUser(Repository):

    def __init__(self, entityType=EntityUser):
        return super().__init__(entityType)

    #用户登录
    def login(self,userName,userPwd):

        return self.findEntity(and_(EntityUser.user_name == userName,EntityUser.user_pwd == Utils.MD5(userPwd)))

    #获取用户列表
    def getUserList(self):
        return self.findList()

    #获取用户详情信息
    def getUserInfo(self,userId):
        return self.findEntity(userId)

#试验业务类
class BllExperiment(Repository):

    def __init__(self, entityType=EntityExperiment):
        return super().__init__(entityType)
    #新增试验
    def insertExperiment(self,experiment_id,experiment_type,material_name,set_temp,description,user_id,user_name, sampling_reminder, set_light):
        entity = EntityExperiment()
        entity.experiment_id = experiment_id
        entity.experiment_type = experiment_type
        entity.material_name = material_name
        entity.set_temp = set_temp
        entity.description = description
        entity.experiment_user_id = user_id
        entity.experiment_user_name = user_name
        entity.sampling_reminder = sampling_reminder
        entity.set_light = set_light
        #time_local = time.localtime(startTimeStamp)
        entity.start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        #time.strftime("%Y-%m-%d %H:%M:%S",time_local)
        entity.end_time = ""
        entity.result = ""
        entity.is_visible = 0
        self.insert(entity)

    #更新试验
    def updateExperiment(self,experiment_id,total_cost,result):
        entity = self.findEntity(experiment_id)
        entity.total_cost = total_cost
        entity.end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        entity.result = result
        entity.is_visible = 1
        self.update(entity)

    #获取试验列表
    def getExperimentList(self):
        return self.findList(EntityExperiment.is_visible == 1)

    #获取试验信息
    def getExperimentAllInfo(self,experiment_id,mainPath):
        experimentInfo = BllExperiment().findEntity(experiment_id)

        tempList=[]
        tempPath= mainPath +'/Resource/ExperimentTemp/{0}'.format(experiment_id)
        for x in range(1,6):
            currentTempTxt=open('{0}/{1}.json'.format(tempPath,x))
            tempjson=currentTempTxt.read()
            tempList.append(tempjson)
            currentTempTxt.close()
        return [experimentInfo,tempList]


#试验记录业务类
class BllExperimentRecord(Repository):

    def __init__(self, entityType=EntityExperimentRecord):
        return super().__init__(entityType)
    #新增试验记录信息
    def insertExperimentRecord(self,record_id,experiment_step_id,experiment_id,temp_value,record_time):
        entity = EntityExperimentRecord()
        entity.record_id = record_id
        entity.experiment_step_id = experiment_step_id
        entity.experiment_id = experiment_id
        entity.temp_value = temp_value
        entity.record_time = record_time
        self.insert(entity)
    #获取试验记录信息
    def getExperimentRecord(self,experiment_id):
        return self.findList(EntityExperimentRecord.experiment_id == experiment_id)

